package com.lz.rocketmq.producer.project.controller;


import cn.hutool.json.JSONUtil;
import com.lz.rocketmq.producer.common.entity.RequestVo;
import com.lz.rocketmq.producer.common.entity.ResponseVo;
import com.lz.rocketmq.producer.common.utils.DateUtils;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;

import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * MQ生产者
 * @author Administrator
 */
@RestController
@RequestMapping(value = "/calc")
public class CalcController {

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @PostMapping("/send")
    public String send(@RequestBody RequestVo requestVo) {
        //requestVo.setRequest("market");
        String msg = JSONUtil.toJsonStr(requestVo);
        Message rocketMsg = MessageBuilder.withPayload(msg).setHeader("TAGS", "ttaggg").build();

        Message rocketMsg2 = MessageBuilder.withPayload(msg+"02").setHeader("TAGS", "tag002").build();
        Message rocketMsg3 = MessageBuilder.withPayload(msg+"03").setHeader("TAGS", "tag003").build();

        //同步发送
        rocketMQTemplate.send("market-topic", rocketMsg);
        SendCallback sendCallback = new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(DateUtils.getCurr_YYYY_MM_DD_HH_MM_SS_SSS()+":requestVo 发送成功");
            }
            @Override
            public void onException(Throwable throwable) {
                System.out.println(DateUtils.getCurr_YYYY_MM_DD_HH_MM_SS_SSS()+":异常 "+throwable.getMessage());

            }
        };
        //异步发送
        rocketMQTemplate.asyncSend("tag-topic:tag002", rocketMsg2,sendCallback);
        rocketMQTemplate.asyncSend("tag-topic:tag003", rocketMsg3,sendCallback);
        //异步发送
        rocketMQTemplate.asyncSend("exp-topic", rocketMsg,sendCallback);
        //异步发送
        rocketMQTemplate.asyncSend("market-topic", rocketMsg,sendCallback);
        //异步发送 延时发送  18 个延迟级别 1s 5s 30s 1m
        rocketMQTemplate.asyncSend("delay-topic", rocketMsg,sendCallback,100000,3);

        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);
        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);
        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);
        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);
        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);
        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);
        rocketMQTemplate.asyncSend("trade-topic", rocketMsg,sendCallback);

        //顺序发送到指定队列
        rocketMQTemplate.asyncSendOrderly("order-topic",msg+"001", "order",sendCallback);
        rocketMQTemplate.asyncSendOrderly("order-topic",msg+"002", "order",sendCallback);
        rocketMQTemplate.asyncSendOrderly("order-topic",msg+"003", "order",sendCallback);
        rocketMQTemplate.asyncSendOrderly("order-topic",msg+"004", "order",sendCallback);
        rocketMQTemplate.asyncSendOrderly("order-topic",msg+"005", "order",sendCallback);
        rocketMQTemplate.asyncSendOrderly("order-topic",msg+"006", "order",sendCallback);


        return "Message:  send  发送成功";
    }

    @PostMapping("/send2")
    public String send2(@RequestBody RequestVo requestVo) {
        requestVo.setRequest("trade");
        String t = JSONUtil.toJsonStr(requestVo);
        rocketMQTemplate.convertAndSend("my-topic", t);
        System.out.println("trade 发送成功");
        return "Message:  send2  发送成功";
    }


    @PostMapping("/sendTag")
    public ResponseVo sendTag(@RequestBody RequestVo requestVo) {
        Message<String> rocketMsg = MessageBuilder.withPayload(JSONUtil.toJsonStr(requestVo)).setHeader("TAGS", "ttaggg").build();
        rocketMQTemplate.convertAndSend("my-topic", rocketMsg);
        return ResponseVo.success(JSONUtil.toJsonStr(requestVo));
    }


}